home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 15 / BBS in a box XV-2.iso / Files II / Prog / B-C / B-YACC.sit / berkeley-yacc-mpw / skeleton.c / skeleton.c
Encoding:
C/C++ Source or Header  |  1991-10-15  |  9.8 KB  |  331 lines  |  [TEXT/MPS ]

  1. /*
  2.  * Copyright (c) 1989 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Robert Paul Corbett.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #ifndef lint
  38. static char sccsid[] = "@(#)skeleton.c    5.6 (Berkeley) 1/27/91";
  39. #endif /* not lint */
  40.  
  41. #include "defs.h"
  42.  
  43. /*  The banner used here should be replaced with an #ident directive    */
  44. /*  if the target C compiler supports #ident directives.        */
  45. /*                                    */
  46. /*  If the skeleton is changed, the banner should be changed so that    */
  47. /*  the altered version can easily be distinguished from the original.    */
  48.  
  49. char *banner[] =
  50. {
  51.     "#ifndef lint",
  52.     "static char yysccsid[] = \"@(#)yaccpar    1.8 (Berkeley) 01/20/90\";",
  53.     "#endif",
  54.     "#define YYBYACC 1",
  55.     0
  56. };
  57.  
  58.  
  59. char *tables[] =
  60. {
  61.     "extern short yylhs[];",
  62.     "extern short yylen[];",
  63.     "extern short yydefred[];",
  64.     "extern short yydgoto[];",
  65.     "extern short yysindex[];",
  66.     "extern short yyrindex[];",
  67.     "extern short yygindex[];",
  68.     "extern short yytable[];",
  69.     "extern short yycheck[];",
  70.     "#if YYDEBUG",
  71.     "extern char *yyname[];",
  72.     "extern char *yyrule[];",
  73.     "#endif",
  74.     0
  75. };
  76.  
  77.  
  78. char *header[] =
  79. {
  80.     "#define yyclearin (yychar=(-1))",
  81.     "#define yyerrok (yyerrflag=0)",
  82.     "#ifdef YYSTACKSIZE",
  83.     "#ifndef YYMAXDEPTH",
  84.     "#define YYMAXDEPTH YYSTACKSIZE",
  85.     "#endif",
  86.     "#else",
  87.     "#ifdef YYMAXDEPTH",
  88.     "#define YYSTACKSIZE YYMAXDEPTH",
  89.     "#else",
  90.     "#define YYSTACKSIZE 500",
  91.     "#define YYMAXDEPTH 500",
  92.     "#endif",
  93.     "#endif",
  94.     "int yydebug;",
  95.     "int yynerrs;",
  96.     "int yyerrflag;",
  97.     "int yychar;",
  98.     "short *yyssp;",
  99.     "YYSTYPE *yyvsp;",
  100.     "YYSTYPE yyval;",
  101.     "YYSTYPE yylval;",
  102.     "short yyss[YYSTACKSIZE];",
  103.     "YYSTYPE yyvs[YYSTACKSIZE];",
  104.     "#define yystacksize YYSTACKSIZE",
  105.     0
  106. };
  107.  
  108.  
  109. char *body[] =
  110. {
  111.     "#define YYABORT goto yyabort",
  112.     "#define YYACCEPT goto yyaccept",
  113.     "#define YYERROR goto yyerrlab",
  114.     "int",
  115.     "yyparse()",
  116.     "{",
  117.     "    register int yym, yyn, yystate;",
  118.     "#if YYDEBUG",
  119.     "    register char *yys;",
  120.     "    extern char *getenv();",
  121.     "",
  122.     "    if (yys = getenv(\"YYDEBUG\"))",
  123.     "    {",
  124.     "        yyn = *yys;",
  125.     "        if (yyn >= '0' && yyn <= '9')",
  126.     "            yydebug = yyn - '0';",
  127.     "    }",
  128.     "#endif",
  129.     "",
  130.     "    yynerrs = 0;",
  131.     "    yyerrflag = 0;",
  132.     "    yychar = (-1);",
  133.     "",
  134.     "    yyssp = yyss;",
  135.     "    yyvsp = yyvs;",
  136.     "    *yyssp = yystate = 0;",
  137.     "",
  138.     "yyloop:",
  139.     "    if (yyn = yydefred[yystate]) goto yyreduce;",
  140.     "    if (yychar < 0)",
  141.     "    {",
  142.     "        if ((yychar = yylex()) < 0) yychar = 0;",
  143.     "#if YYDEBUG",
  144.     "        if (yydebug)",
  145.     "        {",
  146.     "            yys = 0;",
  147.     "            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
  148.     "            if (!yys) yys = \"illegal-symbol\";",
  149.     "            printf(\"yydebug: state %d, reading %d (%s)\\n\", yystate,",
  150.     "                    yychar, yys);",
  151.     "        }",
  152.     "#endif",
  153.     "    }",
  154.     "    if ((yyn = yysindex[yystate]) && (yyn += yychar) >= 0 &&",
  155.     "            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)",
  156.     "    {",
  157.     "#if YYDEBUG",
  158.     "        if (yydebug)",
  159.     "            printf(\"yydebug: state %d, shifting to state %d\\n\",",
  160.     "                    yystate, yytable[yyn]);",
  161.     "#endif",
  162.     "        if (yyssp >= yyss + yystacksize - 1)",
  163.     "        {",
  164.     "            goto yyoverflow;",
  165.     "        }",
  166.     "        *++yyssp = yystate = yytable[yyn];",
  167.     "        *++yyvsp = yylval;",
  168.     "        yychar = (-1);",
  169.     "        if (yyerrflag > 0)  --yyerrflag;",
  170.     "        goto yyloop;",
  171.     "    }",
  172.     "    if ((yyn = yyrindex[yystate]) && (yyn += yychar) >= 0 &&",
  173.     "            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)",
  174.     "    {",
  175.     "        yyn = yytable[yyn];",
  176.     "        goto yyreduce;",
  177.     "    }",
  178.     "    if (yyerrflag) goto yyinrecovery;",
  179.     "#ifdef lint",
  180.     "    goto yynewerror;",
  181.     "#endif",
  182.     "yynewerror:",
  183.     "    yyerror(\"syntax error\");",
  184.     "#ifdef lint",
  185.     "    goto yyerrlab;",
  186.     "#endif",
  187.     "yyerrlab:",
  188.     "    ++yynerrs;",
  189.     "yyinrecovery:",
  190.     "    if (yyerrflag < 3)",
  191.     "    {",
  192.     "        yyerrflag = 3;",
  193.     "        for (;;)",
  194.     "        {",
  195.     "            if ((yyn = yysindex[*yyssp]) && (yyn += YYERRCODE) >= 0 &&",
  196.     "                    yyn <= YYTABLESIZE && yycheck[yyn] == YYERRCODE)",
  197.     "            {",
  198.     "#if YYDEBUG",
  199.     "                if (yydebug)",
  200.     "                    printf(\"yydebug: state %d, error recovery shifting\\",
  201.     " to state %d\\n\", *yyssp, yytable[yyn]);",
  202.     "#endif",
  203.     "                if (yyssp >= yyss + yystacksize - 1)",
  204.     "                {",
  205.     "                    goto yyoverflow;",
  206.     "                }",
  207.     "                *++yyssp = yystate = yytable[yyn];",
  208.     "                *++yyvsp = yylval;",
  209.     "                goto yyloop;",
  210.     "            }",
  211.     "            else",
  212.     "            {",
  213.     "#if YYDEBUG",
  214.     "                if (yydebug)",
  215.     "                    printf(\"yydebug: error recovery discarding state %d\
  216. \\n\",",
  217.     "                            *yyssp);",
  218.     "#endif",
  219.     "                if (yyssp <= yyss) goto yyabort;",
  220.     "                --yyssp;",
  221.     "                --yyvsp;",
  222.     "            }",
  223.     "        }",
  224.     "    }",
  225.     "    else",
  226.     "    {",
  227.     "        if (yychar == 0) goto yyabort;",
  228.     "#if YYDEBUG",
  229.     "        if (yydebug)",
  230.     "        {",
  231.     "            yys = 0;",
  232.     "            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
  233.     "            if (!yys) yys = \"illegal-symbol\";",
  234.     "            printf(\"yydebug: state %d, error recovery discards token %d\
  235.  (%s)\\n\",",
  236.     "                    yystate, yychar, yys);",
  237.     "        }",
  238.     "#endif",
  239.     "        yychar = (-1);",
  240.     "        goto yyloop;",
  241.     "    }",
  242.     "yyreduce:",
  243.     "#if YYDEBUG",
  244.     "    if (yydebug)",
  245.     "        printf(\"yydebug: state %d, reducing by rule %d (%s)\\n\",",
  246.     "                yystate, yyn, yyrule[yyn]);",
  247.     "#endif",
  248.     "    yym = yylen[yyn];",
  249.     "    yyval = yyvsp[1-yym];",
  250.     "    switch (yyn)",
  251.     "    {",
  252.     0
  253. };
  254.  
  255.  
  256. char *trailer[] =
  257. {
  258.     "    }",
  259.     "    yyssp -= yym;",
  260.     "    yystate = *yyssp;",
  261.     "    yyvsp -= yym;",
  262.     "    yym = yylhs[yyn];",
  263.     "    if (yystate == 0 && yym == 0)",
  264.     "    {",
  265.     "#if YYDEBUG",
  266.     "        if (yydebug)",
  267.     "            printf(\"yydebug: after reduction, shifting from state 0 to\\",
  268.     " state %d\\n\", YYFINAL);",
  269.     "#endif",
  270.     "        yystate = YYFINAL;",
  271.     "        *++yyssp = YYFINAL;",
  272.     "        *++yyvsp = yyval;",
  273.     "        if (yychar < 0)",
  274.     "        {",
  275.     "            if ((yychar = yylex()) < 0) yychar = 0;",
  276.     "#if YYDEBUG",
  277.     "            if (yydebug)",
  278.     "            {",
  279.     "                yys = 0;",
  280.     "                if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
  281.     "                if (!yys) yys = \"illegal-symbol\";",
  282.     "                printf(\"yydebug: state %d, reading %d (%s)\\n\",",
  283.     "                        YYFINAL, yychar, yys);",
  284.     "            }",
  285.     "#endif",
  286.     "        }",
  287.     "        if (yychar == 0) goto yyaccept;",
  288.     "        goto yyloop;",
  289.     "    }",
  290.     "    if ((yyn = yygindex[yym]) && (yyn += yystate) >= 0 &&",
  291.     "            yyn <= YYTABLESIZE && yycheck[yyn] == yystate)",
  292.     "        yystate = yytable[yyn];",
  293.     "    else",
  294.     "        yystate = yydgoto[yym];",
  295.     "#if YYDEBUG",
  296.     "    if (yydebug)",
  297.     "        printf(\"yydebug: after reduction, shifting from state %d \\",
  298.     "to state %d\\n\", *yyssp, yystate);",
  299.     "#endif",
  300.     "    if (yyssp >= yyss + yystacksize - 1)",
  301.     "    {",
  302.     "        goto yyoverflow;",
  303.     "    }",
  304.     "    *++yyssp = yystate;",
  305.     "    *++yyvsp = yyval;",
  306.     "    goto yyloop;",
  307.     "yyoverflow:",
  308.     "    yyerror(\"yacc stack overflow\");",
  309.     "yyabort:",
  310.     "    return (1);",
  311.     "yyaccept:",
  312.     "    return (0);",
  313.     "}",
  314.     0
  315. };
  316.  
  317.  
  318. write_section(section)
  319. char *section[];
  320. {
  321.     register int i;
  322.     register FILE *fp;
  323.  
  324.     fp = code_file;
  325.     for (i = 0; section[i]; ++i)
  326.     {
  327.     ++outline;
  328.     fprintf(fp, "%s\n", section[i]);
  329.     }
  330. }
  331.